package com.example.rabc.service.impl;

import com.example.rabc.dao.ProductRepository;
import com.example.rabc.entity.Product;
import com.example.rabc.exception.BusinessException;
import com.example.rabc.monitor.Monitored;
import com.example.rabc.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 产品服务实现类
 */
@Service
@Transactional
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductRepository productRepository;

    @Override
    public Product createProduct(Product product) {
        // 检查产品代码是否已存在
        if (productRepository.existsByProductCode(product.getProductCode())) {
            throw new BusinessException("产品代码已存在");
        }

        return productRepository.save(product);
    }

    @Override
    @Monitored
    public Product updateProduct(Long productId, Product product) {
        Product existingProduct = productRepository.findById(productId)
                .orElseThrow(() -> new BusinessException("产品不存在"));

        // 检查产品代码是否被其他产品使用
        if (!existingProduct.getProductCode().equals(product.getProductCode())) {
            if (productRepository.existsByProductCode(product.getProductCode())) {
                throw new BusinessException("产品代码已存在");
            }
        }

        // 更新产品信息
        existingProduct.setProductName(product.getProductName());
        existingProduct.setProductCode(product.getProductCode());
        existingProduct.setDescription(product.getDescription());
        existingProduct.setPrice(product.getPrice());
        existingProduct.setStockQuantity(product.getStockQuantity());
        existingProduct.setStatus(product.getStatus());

        return productRepository.save(existingProduct);
    }

    @Override
    @Monitored
    public void deleteProduct(Long productId) {
        if (!productRepository.existsById(productId)) {
            throw new BusinessException("产品不存在");
        }
        productRepository.deleteById(productId);
    }

    @Override
    @Monitored
    @Transactional(readOnly = true)
    public Product getProductById(Long productId) {
        return productRepository.findById(productId)
                .orElseThrow(() -> new BusinessException("产品不存在"));
    }

    @Override
    @Monitored
    @Transactional(readOnly = true)
    public Product getProductByCode(String productCode) {
        return productRepository.findByProductCode(productCode)
                .orElseThrow(() -> new BusinessException("产品不存在"));
    }

    @Override
    @Monitored
    @Transactional(readOnly = true)
    public List<Product> getAllProducts() {
        return productRepository.findAll();
    }

    @Override
    @Monitored
    @Transactional(readOnly = true)
    public List<Product> getAvailableProducts() {
        return productRepository.findAll().stream()
                .filter(product -> product.getStatus() == 1)
                .collect(Collectors.toList());
    }
}